package org.qydjk.h5.controller.seller;

import org.qydjk.common.constant.RedisConstant;
import org.qydjk.common.pojo.Area;
import org.qydjk.common.pojo.query.QuerySeller;
import org.qydjk.common.pojo.query.QueryUserComment;
import org.qydjk.common.util.RedisTemplateUtil;
import org.qydjk.common.vo.PaginationInfo;
import org.qydjk.common.vo.redisvo.RedisSellerProductLabel;
import org.qydjk.h5.controller.base.BaseController;
import org.qydjk.service.service.area.IAreaService;
import org.qydjk.service.service.customerCenter.ICustomerCollectionService;
import org.qydjk.service.service.customerCenter.ICustomerCommentService;
import org.qydjk.service.service.product.IProductService;
import org.qydjk.service.service.seller.ISellerService;
import org.qydjk.service.vo.product.ProductListParam;
import org.qydjk.service.vo.seller.SellerListParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

/**
 * 商家Controller
 * Created by Edward_yang on 2018/1/3 0003.
 */
@RequestMapping("/wap/seller")
@Controller
public class SellerController extends BaseController
{
    @Autowired
    private ISellerService sellerService;
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    @Autowired
    private IProductService productService;
    @Autowired
    private IAreaService areaService;
    @Autowired
    private ICustomerCollectionService customerCollectionService;
    @Autowired
    private ICustomerCommentService customerCommentService;
    /***
     * 前往商家列表
     * @author Edward_yang
     * @uptime 2018-01-03
     * @return
     */
    @RequestMapping("/gotoSellerList.do")
    public String  gotoSellerList(){
        //Redis后台初始化
        List<RedisSellerProductLabel> sLabelList = (List<RedisSellerProductLabel>) redisTemplateUtil.getWithByte(RedisConstant.SELLER_LABEL_KEY);
        //初始化商家标签数据
        request.setAttribute("sLabelList",sLabelList);

        Integer cityId = 2;
        try {
            List<Area> areaList = areaService.findAreaByParentId(cityId);
            request.setAttribute("areaList",areaList);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "index/sellerList";
    }

    /***
     * 商家列表查询
     * @author Edward_yang
     * @uptime 2018-01-04
     * @return
     */
    @RequestMapping("/sellerList.do")
    @ResponseBody
    public Object  sellerList(Integer page, Integer rows,Double lat,Double lon,Integer cityId,  Integer areaId,String sCode,Integer  type){
        SellerListParam   sellerListParam= new  SellerListParam();
        try {
            if(rows==null){
                rows=5 ;
                sellerListParam.setPage(page);
                sellerListParam.setPageSize(rows);
            }
          if (type==null){
              type=0;
          }
              switch (type) {
                  case 0://综合
                      sellerListParam.setOrdColumn("ord");
                      break;
                  case 1://评分
                      sellerListParam.setOrdColumn("commentScore");
                      break;
                  case 2://距离
                      sellerListParam.setOrdColumn("distance");
                      break;
              }
            sellerListParam.setOrdWay("DESC");//倒序
            sellerListParam.setLat(lat);
            sellerListParam.setLon(lon);
            sellerListParam.setCityId(cityId);
            sellerListParam.setAreaId(areaId);
            sellerListParam.setSellerTag(sCode);
            //TODO sellerListParam.iscont() 默认 false 不统计条数
            PaginationInfo sellerList= sellerService.findSellerList(sellerListParam);
            return renderListSuccess(sellerList.getList(),rows);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     *前往商家的详情信息
     * @author Edward_yang
     * @uptime 2018-01-09
     * @return
     */
    @RequestMapping("gotoSellerDetail.do")
    public String  gotoSellerDetail(Integer sellerId){
        try {
             request.setAttribute("querySeller",sellerService.findQuerySeller(sellerId));
             request.setAttribute("specialistList",sellerService.findSpecialistList(sellerId));
             request.setAttribute("hospitalKeyDepartmentLsit",sellerService.findHospitalKeyDepartmentLsit(sellerId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/index/sellerDetail";
    }

    /***
     *商家的详情套餐
     * @author Edward_yang
     * @uptime 2018-01-09
     * @return
     */
    @RequestMapping("sellerProductList.do")
    @ResponseBody
    public Object sellerProductList(Integer page, Integer rows,Integer sellerId,Integer dataType){
        if(rows==null){
            rows=5 ;
        }
        //TODO 第一个tabs 不请求数据做阻塞
        if(dataType==null||dataType==1){
            return renderSuccess();
        }
        ProductListParam productListParam=new ProductListParam();
        productListParam.setPage(page);
        productListParam.setPageSize(rows);
        productListParam.setSellerId(sellerId);
        productListParam.setOrdWay("DESC");
        switch (dataType){
            case 2:
                //综合排序
                productListParam.setOrdColumn("ord");
                break;
            case 3:
                //上架时间
                productListParam.setOrdColumn("update_time");

                break;
            case 4:
                //销量
                productListParam.setOrdColumn("sales_count");
                break;
            case 5:
                //TODO 评分 isDummy 是否虚假评论：0否 1是，null 全部
                try {
                    List<QueryUserComment> queryUserCommentList=customerCommentService.findCustomerCommentList( page,  rows, sellerId,null,null);
                    return renderListSuccess(queryUserCommentList, rows);
                } catch (Exception e) {
                    e.printStackTrace();
                    return renderError(e.getMessage());
                }
        }
        try {

            PaginationInfo productList = productService.findProductList(productListParam);
            return renderListSuccess(productList.getList(), rows);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }

    }

   

    /***
     * 点击收藏 /取消收藏
     * @author Edward_yang
     * @uptime 2018-01-10
     */
    @RequestMapping("onclickSellerCollect.do")
    @ResponseBody
    public Object onclickSellerCollect(Integer userId,Integer sellerId ,Boolean isCollect){
            userId=1;
        try {
            //TODO type 收藏类型：0产品1商家2文章
            //TODO isCollect 取反
            customerCollectionService.addonclickUserCollect(userId,sellerId,1,isCollect);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }

    }


}
